﻿#include "curveconfigprocess.h"

CurveConfigProcess::CurveConfigProcess(QObject *parent) : QObject(parent)
{
    pCurvesList = new QVector<QString>;
}

CurveConfigProcess::~CurveConfigProcess()
{
    delete pCurvesList;
}


void CurveConfigProcess::getConfig(QString filepath)
{
    // 将xml文件读取数据
    QFile file(filepath);

    pCurvesList->clear();

    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
      qDebug()<<"error";
    }

    QXmlStreamReader reader;

    // 指定读取设备
    reader.setDevice(&file);

    while(!reader.atEnd())
    {
        // 读取下一个标签
        reader.readNext();
        // 解析内容
        if(reader.isStartElement())
        {
            // 解析
            if(reader.name() == QString("VarName"))
            {
                pCurvesList->append(reader.readElementText());
            }
        }
    }

    return;
}



void CurveConfigProcess::saveConfig(QVector<PDATA_CONTEXT> curveslist, QString filepath)
{
    QFile file(filepath);

    if (!file.open(QFile::WriteOnly | QFile::Text))
    {
      qDebug()<<"error";
    }

    QXmlStreamWriter writer;

    // 指定读取设备
    writer.setDevice(&file);

    writer.setAutoFormatting(true);
    writer.writeStartDocument();
    writer.writeStartElement("Config");

    for (auto &item : std::as_const(curveslist)) {
        writer.writeTextElement("VarName", *item->title);
    }
//    for(QVector<PDATA_CONTEXT>::iterator it = curveslist.begin(); it != curveslist.end(); ++it)
//    {
//        writer.writeTextElement("VarName", *(*it)->title);
//    }
    writer.writeEndElement();
    writer.writeEndDocument();
    file.close();

    return;
}


QVector<QString>* CurveConfigProcess::getCurves()
{
    return pCurvesList;
}


void CurveConfigProcess::clearCurvesList()
{
    if(pCurvesList)
    {
        pCurvesList->clear();
    }

    return;
}



